package pers.yurwisher.rubick.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.yurwisher.rubick.common.utils.ObjectHelper;


/**
 * @author yq
 * @date 2021年4月14日 11:25:49
 * @description mybatis-plus config
 * @since V1.0.0
 */
@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class, MybatisPlusProperties.class})
@Slf4j
@AutoConfigureAfter({MybatisPlusAutoConfiguration.class})
public class CustomMybatisPlusConfig {

    public CustomMybatisPlusConfig() {
        log.info("auto inject mybatisPlusInterceptor,metaObjectHandler");
    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 自动填充值
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return ObjectHelper.getDefaultMetaObjectHandler();
    }
}
